package com.anyou.mall.order.orderServe.service.impl;

import java.util.List;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.exception.GlobalException;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.goo.goods.domain.GooGoods;
import com.anyou.mall.goo.goods.domain.LiGoodsSku;
import com.anyou.mall.goo.goods.mapper.GooGoodsMapper;
import com.anyou.mall.goo.goods.mapper.LiGoodsSkuMapper;
import com.anyou.mall.goo.goodsSku.domain.GooGoodsSku;
import com.anyou.mall.goo.goodsSku.mapper.GooGoodsSkuMapper;
import com.anyou.mall.order.domain.OrdOrder;
import com.anyou.mall.order.mapper.OrdOrderMapper;
import com.anyou.mall.order.orderGoods.domain.OrdOrderGoods;
import com.anyou.mall.order.orderGoods.mapper.OrdOrderGoodsMapper;
import com.anyou.mall.order.orderServe.vo.OrderPayVo;
import com.anyou.mall.order.orderServe.vo.PayVo;
import com.anyou.mall.wxrefund.WxRefundParam;
import com.anyou.mall.wxrefund.WxRefundService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.order.orderServe.mapper.OrdOrderServeMapper;
import com.anyou.mall.order.orderServe.domain.OrdOrderServe;
import com.anyou.mall.order.orderServe.service.IOrdOrderServeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单售后Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-23
 */
@Service
public class OrdOrderServeServiceImpl extends ServiceImpl<OrdOrderServeMapper, OrdOrderServe> implements IOrdOrderServeService
{
    @Autowired
    private OrdOrderServeMapper ordOrderServeMapper;

    //订单商品
    @Autowired
    private OrdOrderGoodsMapper ordOrderGoodsMapper;

    @Autowired
    private OrdOrderMapper ordOrderMapper;

    @Autowired
    private GooGoodsSkuMapper gooGoodsSkuMapper;
    @Autowired
    private LiGoodsSkuMapper liGoodsSkuMapper;

    @Autowired
    private GooGoodsMapper gooGoodsMapper;

    @Autowired
    private WxRefundService wxRefundService;



    /**
     * 查询订单售后
     *
     * @param orderServeId 订单售后主键
     * @return 订单售后
     */
    @Override
    public OrdOrderServe selectOrdOrderServeByOrderServeId(Long orderServeId)
    {
        OrdOrderServe orderServe = ordOrderServeMapper.selectById(orderServeId);

        //查询订单商品
        List<OrdOrderGoods> ordOrderGoods = ordOrderGoodsMapper.selectList(new LambdaQueryWrapper<OrdOrderGoods>()
                .eq(OrdOrderGoods::getOrderNo,orderServe.getServeNo()).eq(OrdOrderGoods::getDelFlag,0));
        ordOrderGoods.forEach(e->{
            LiGoodsSku liGoodsSku = liGoodsSkuMapper.selectById(e.getGoodsSkuId());
            if (liGoodsSku!=null){
                e.setSpecsCompose(liGoodsSku.getSimpleSpecs());
            }
            //查询商品表的图片金额商品名
            if (liGoodsSku!=null){
//                GooGoods gooGoods = gooGoodsMapper.selectById(liGoodsSku.getGoodsId());
//                e.setGoodsName(gooGoods.getGoodsName());
//                e.setGoodsImgUrl(gooGoods.getOriginal());
            }

        });
        return orderServe;
    }

    /**
     * 查询订单售后列表
     *
     * @param ordOrderServe 订单售后
     * @return 订单售后
     */
    @Override
    public List<OrdOrderServe> selectOrdOrderServeList(OrdOrderServe ordOrderServe)
    {
        List<OrdOrderServe> ordOrderServes = ordOrderServeMapper.selectOrdOrderServeList(ordOrderServe);
        ordOrderServes.forEach(e->{
            // 查询用户信息
            e.setUserInfo(ordOrderServeMapper.userInfo(e.getUserId()));
        });


        return ordOrderServes;
    }

    /**
     * 新增订单售后
     *
     * @param ordOrderServe 订单售后
     * @return 结果
     */
    @Override
    public int insertOrdOrderServe(OrdOrderServe ordOrderServe)
    {
        ordOrderServe.setCreateTime(DateUtils.getNowDate());
        return ordOrderServeMapper.insertOrdOrderServe(ordOrderServe);
    }

    /**
     * 修改订单售后
     *
     * @param ordOrderServe 订单售后
     * @return 结果
     */
    @Override
    public int updateOrdOrderServe(OrdOrderServe ordOrderServe)
    {
        ordOrderServe.setUpdateTime(DateUtils.getNowDate());
        return ordOrderServeMapper.updateOrdOrderServe(ordOrderServe);
    }

    /**
     * 批量删除订单售后
     *
     * @param orderServeIds 需要删除的订单售后主键
     * @return 结果
     */
    @Override
    public int deleteOrdOrderServeByOrderServeIds(Long[] orderServeIds)
    {
        return ordOrderServeMapper.deleteOrdOrderServeByOrderServeIds(orderServeIds);
    }

    /**
     * 删除订单售后信息
     *
     * @param orderServeIds 订单售后主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteOrdOrderServeByOrderServeId(Long[] orderServeIds)
    {
        for (Long serveId : orderServeIds) {
            OrdOrderServe os = ordOrderServeMapper.selectById(serveId);
            int i = ordOrderServeMapper.deleteById(serveId);
            if(i>0&&os!=null){
                ordOrderGoodsMapper.deleteOrdOrderGoodsByOrderGoodsId(os.getServeNo());
            }

        }

        return 1;
    }

    @Override
    public ApiResult audit(Long serveId, String serveAuditStatus, String auditRemark) {
        LambdaUpdateWrapper<OrdOrderServe> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(OrdOrderServe::getServeAuditStatus,serveAuditStatus)
                .set(OrdOrderServe::getAuditRemark,auditRemark)
                .eq(OrdOrderServe::getOrderServeId,serveId);

        /*if (serveAuditStatus.equals("1")){
            updateWrapper.set(OrdOrderServe::getServeAuditStatus,serveAuditStatus)
                    .set(OrdOrderServe::getServeStatus,"5")
                    .set(OrdOrderServe::getServeAuditStatus,serveAuditStatus)
                    .set(OrdOrderServe::getAuditRemark,auditRemark)
                    .eq(OrdOrderServe::getOrderServeId,serveId);
        }*/
        //如果是拒绝需要处理两个字段
        if (serveAuditStatus.equals("2")){
            updateWrapper.set(OrdOrderServe::getServeAuditStatus,serveAuditStatus)
                    .set(OrdOrderServe::getServeStatus,"2")
                    .set(OrdOrderServe::getServeAuditStatus,serveAuditStatus)
                    .set(OrdOrderServe::getAuditRemark,auditRemark)
                    .eq(OrdOrderServe::getOrderServeId,serveId);
        }
        return ApiResult.success(ordOrderServeMapper.update(null,updateWrapper));
        /*return ApiResult.success(ordOrderServeMapper.audit(serveNo,serveStatus,auditRemark,userId));*/
    }

    @Override
    public ApiResult finish(Long serveId) {
        OrdOrderServe orderServe = this.getById(serveId);
        //如果是退货退款,就执行退款操作
        if(orderServe!=null&&orderServe .getServeType().equals("1")){
            //查询原支付时的订单号
            //根据商品订单号查询原支付金额,原支付订单号
//            PayVo payVo=this.getYuanOrder(orderServe.getGoodsOrderNo());
//            WxRefundParam wxRefundParam = new WxRefundParam(payVo.getOrderNo(),orderServe.getServeNo(),payVo.getMoney(),orderServe.getRefundAmount().doubleValue());
//            ApiResult apiResult = wxRefundService.wxRefund(wxRefundParam);
//            return apiResult;
        }
        LambdaUpdateWrapper<OrdOrderServe> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(OrdOrderServe::getServeStatus,3)
                .eq(OrdOrderServe::getOrderServeId,serveId);
        return ApiResult.success(ordOrderServeMapper.update(null,updateWrapper));
    }

    @Override
    public ApiResult sendGoods(Long serveId, Long logisticsId,String logisticsName, String logisticsNo) {
        LambdaUpdateWrapper<OrdOrderServe> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(OrdOrderServe::getLogisticsNo,logisticsNo)
                .set(OrdOrderServe::getLogisticsName,logisticsName)
                .set(OrdOrderServe::getSendStatus,1)
                .eq(OrdOrderServe::getOrderServeId,serveId);

       /* ordOrderServeMapper.sendGoods(serveNo,logisticsId,logisticsNo,userId);*/
        return ApiResult.success(ordOrderServeMapper.update(null,updateWrapper));
    }

    @Override
    public ApiResult<OrdOrderServe> getLogisticsInfo(Long orderServeId) {
        //查询售后管理订单物流
        /*OrderServiceLogisticsVO logisticsInfo = ordOrderServeMapper.logisticsInfo(orderServeId);*/
        return ApiResult.success(ordOrderServeMapper.selectById(orderServeId));
    }

    @Override
    public PayVo getYuanOrder(String goodsOrderNo) {
        if(StringUtils.isEmpty(goodsOrderNo)){
            throw new GlobalException("商品订单号不存在");
        }
        PayVo payVo = new PayVo();

        OrdOrder ordOrder = ordOrderMapper.selectOne(new LambdaQueryWrapper<OrdOrder>()
                .eq(OrdOrder::getOrderNo, goodsOrderNo));
        if(ordOrder==null){
            throw new GlobalException("当前商品订单不存在");
        }
        payVo.setOrderNo(goodsOrderNo);
        payVo.setMoney(ordOrder.getOrderAmount().doubleValue());
        if(StringUtils.isNotEmpty(ordOrder.getOrderPayNo())&&!"0".equals(ordOrder.getOrderPayNo())){
            payVo.setOrderNo(ordOrder.getOrderPayNo());
            //查询原支付金额
            OrderPayVo orderPayVo=ordOrderMapper.getOrderPay(ordOrder.getOrderPayNo());
            if(orderPayVo!=null){
                payVo.setMoney(orderPayVo.getOrderAmount().doubleValue());
            }
        }
        return payVo;
    }


}
